<?
class QueryController extends AppController {
    var $uses = array();
	//var $cacheAction = '1 year';
    //var $components = array('RequestHandler');
	var $helpers = array('Cache');
	
	function beforeFilter() {
		Configure::load('versions');		
		$configuration = Configure::read('Versions');
		if(!empty($configuration)) {
			$this->languages = array_keys($configuration);
		}
		
		if(!empty($this->params['language'])) {
			$this->language = $this->params['language'];
			$this->set('language',$this->language);
		}

		if(!empty($this->params['version'])) {
			$this->version = $this->params['version'];
			$this->set('version',$this->version);
		}
		
		if(!empty($this->language) && !empty($this->version) && !empty($this->params['url']['passage'])) {
			$url = '/' . $this->language . '/' . $this->version . '/' . $this->params['url']['passage'];
			$this->redirect(empty($this->params['url']['css']) ? $url : $url . '?css=' . $this->params['url']['css']);
		}
		
		if(!empty($this->params['url']['css'])) {
			$this->set('css',$this->params['url']['css']);
		}
	}
	
	function languages() {
		$this->set('languages',$this->languages);
	}
	
	function versions() {
		$languages = Configure::read('Versions');
		$this->set('versions',$languages[$this->language]);
	}
	
	function books() {
		if($this->language == 'en' && $this->version == 'esv') {
			$this->redirect('http://www.gnpcb.org/esv/mobile/');
		}
		
		App::import('Model','Book');
		DEFINE('BOOKS_TABLE',$this->language . '_' . $this->version . '_books');
		$this->Book = new Book;

		$books = $this->Book->find('all');
		$this->set('books',$books);
	}
    
	function chapter() {
		$passage = $this->params['passage'];
		$this->set('passage',$passage);

		if($this->language == 'en' && $this->version == 'esv') {
			$this->redirect('http://www.gnpcb.org/esv/mobile/?q=' . $passage);
		}
		
		$versions = Configure::read('Versions.' . $this->language);
		if(!in_array($this->version,$versions)) {
			die('Version not supported');
		}
			
		$this->set('title_for_layout',$passage);
		$this->title_for_layout = $passage;
						
		App::import('Vendor','parser');
		$parsed_query = parse_input($passage);

		if(!empty($parsed_query[0][1]['ref'][0]['beginv'])) {
			$begin_verse = $parsed_query[0][1]['ref'][0]['beginv'];
			$this->set('begin_verse',(int) $begin_verse);
		} else {
			$this->set('begin_verse',0);
		}
		
		if(!empty($parsed_query[0][1]['ref'][0]['beginv'])) {
			$end_verse = $parsed_query[0][1]['ref'][0]['endv'];
			$this->set('end_verse',(int) $end_verse);
		} else {
			$this->set('end_verse',0);
		}
		
		App::import('Model','Verse');
		DEFINE('VERSES_TABLE',$this->language . '_' . $this->version . '_verses');
		$this->Verse = new Verse;

		App::import('Model','Book');
		DEFINE('BOOKS_TABLE',$this->language . '_' . $this->version . '_books');
		$this->Book = new Book;
		
		if(empty($parsed_query[0][0]['book'])) {
			die('Book not specified or recognized');
		}
		$book = $parsed_query[0][0]['book'];

		if(empty($parsed_query[0][1]['ref'][0]['beginc'])) {
			die('Chapter not specified or recognized');
		}
		$chapter = $parsed_query[0][1]['ref'][0]['beginc'];
		$this->set('chapter',$chapter);
		
		$verses = $this->Verse->find('all',array(
			'conditions' => array('Verse.book' => $book, 'Verse.chapter' => $chapter),
			'sort' => 'Verse.verse',
			'fields' => array('verse','text')
		));
		$this->set('verses',$verses);
		
		$book_name = $this->Book->field('name',array('Book.id' => $book));
		$this->set('book_name',$book_name);
		
		if($chapter > 1) {
			$this->set('previous_book_name',$book_name);
			$this->set('previous_chapter',$chapter - 1);
		} else if($chapter == 1 && $book > 1) {
			$previous_book_name = $this->Book->field('name',array('Book.id' => $book - 1));
			$this->set('previous_book_name',$previous_book_name);
			$this->set('previous_chapter',$this->totalChaptersInBook[$book - 1]);
		}
		
		if($chapter < $this->totalChaptersInBook[$book]) {
			$this->set('next_book_name',$book_name);
			$this->set('next_chapter',$chapter + 1);
		} else if($chapter == $this->totalChaptersInBook[$book] && $book < 66) {
			$next_book_name = $this->Book->field('name',array('Book.id' => $book + 1));
			$this->set('next_book_name',$next_book_name);
			$this->set('next_chapter',1);
		}

		$this->set('title',$passage);
	}
	
	var $totalChaptersInBook = array(
		1 => 50,
		2 => 40,
		3 => 27,
		4 => 36,
		5 => 34,
		6 => 24,
		7 => 21,	
		8 => 4,
		9 => 31,
		10 => 24,
		11 => 22,
		12 => 25,
		13 => 29,
		14 => 36,
		15 => 10,
		16 => 13,
		17 => 10,	
		18 => 42,	
		19 => 150,
		20 => 31,
		21 => 12,
		22 => 8,
		23 => 66,
		24 => 52,
		25 => 5,
		26 => 48,
		27 => 14,
		28 => 14,
		29 => 3,
		30 => 9,
		31 => 1,
		32 => 4,	
		33 => 7,
		34 => 3,
		35 => 3,
		36 => 3,
		37 => 2,
		38 => 14,
		39 => 3,
		40 => 28,
		41 => 16,
		42 => 24,
		43 => 21,
		44 => 28,
		45 => 16,
		46 => 16,
		47 => 13,
		48 => 6,
		49 => 6,
		50 => 4,
		51 => 4,
		52 => 5,
		53 => 3,
		54 => 6,
		55 => 4,
		56 => 3,
		57 => 1,
		58 => 13,
		59 => 5,
		60 => 5,
		61 => 3,
		62 => 5,
		63 => 1,
		64 => 1,
		65 => 1,
		66 => 22
	);
}